package com.it.demo.servlet;

import com.it.demo.dao.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginServlet() {
        super();
        System.out.println("LoginServlet...");
    }

    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("init...");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Enumeration<String> enum1 = request.getHeaderNames();// 获取请求的 头信息
        while (enum1.hasMoreElements()) {
            String name = enum1.nextElement();
            String value = request.getHeader(name);
            System.out.println(name + ":" + value);
        }

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        UserDaoImpl daoImpl = new UserDaoImpl();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.err.println("账号与密码  " + username + " : " + password);
        boolean b = daoImpl.login(username, password);
        if (b) {
            out.print("<h1>登录成功</h1>");
        } else {
            out.print("账号密码错误...账号:root 密码: 123");
        }

    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("destroy...");
    }

}
